﻿<?php

/**
 * Copyright (c) 2012, Jiehun.com.cn Inc. All Rights Reserved
 * @author zhanghaozheng@jiehun.com.cn
 * @date 2012-2-13
 * @version 1.0
 * @brief
 *
 **/
final class Mail
{
	var $host;
	var $port;
	var $passwd;
	var $userName;
	var $from;
	var $fromName;
	
	var $mail;
	var $logFunc;
	var $conf;
	
	function __construct($conf)
	{
		$this->conf = $conf;
		$this->setConf($this->conf);
	}

	/**
	*设置配置中的通道
	*/
	function setUseConf($useName)
	{
		if(empty($this->conf[$useName])) throw new Exception('mail,u_setUse userName not found');
		$this->setConf($this->conf[$useName]);
	}

	/**
	*设置自定义的配置通道
	*/
	function setCustomConf(array $conf)
	{
		$conf = array_filter($conf);
		if( array_diff(array('host','user_name','password','port','from','from_name'),array_keys($conf)) ) {
			throw new Exception('mail,u_setConf illegality');
		}
		$this->setConf($conf);
	}

	/**
	*设置配置
	*/
	private function setConf($conf=array())
	{
		$keys = array_keys($conf);
		if(empty($keys) || array_diff(array('host','user_name','password','port','from','from_name'),$keys))
		{
			throw new Exception('mail.u_mail setConf error');
		}

		$this->host = $conf['host'];
		$this->userName = $conf['user_name'];
		$this->password = $conf['password'];
		$this->port = $conf['port'];
		$this->from = $conf['from'];
		$this->fromName = $conf['from_name'];
		$this->logFunc = (!empty($conf['log_func'])&&is_callable($conf['log_func']))?$conf['log_func']:null;

		require_once dirname(__FILE__).'/class.phpmailer.php';
		$this->mail = new PHPMailer();
		$this->mail->IsSMTP();
		//$this->mail->CharSet   = "UTF-8"; 
		$this->mail->SMTPAuth = true;         
		$this->mail->Host = $this->host;      
		$this->mail->Port       = $this->port;      
		$this->mail->Username   = $this->userName;  
		$this->mail->Password   = $this->password;  
		$this->mail->From = $this->from;
		$this->mail->FromName = $this->fromName;
	}

	/**
	*发送邮件
	*@param $mailTo 收件人 可为数组
	*@param $subject 主题
	*@param $content 内容
	*@return true or false
	*/
	function send($mailTo,$subject,$content,$isHtml=true)
	{
		$this->mail->Subject    = "=?utf-8?B?".base64_encode($subject)."?=";
		$this->mail->WordWrap   = 80; 
		if($isHtml) $this->mail->IsHTML(true);
		$this->mail->ClearAddresses();
		if(is_array($mailTo))
		{
			foreach($mailTo as $m)
			{
				if($m)
				{
					$this->mail->AddAddress($m);
				}
			}
		}else
		{
			$this->mail->AddAddress($mailTo);
		}

		//特殊处理
		//$this->filterMail($mailTo,$content);

		$this->mail->MsgHTML($content);
		global $__HapN_appid;
		if ($__HapN_appid) {
			//在HapN框架的一个特殊支持，不用再HapN下也无影响
			$__HapN_appid ++;
		}
		if(!($this->mail->Send()))
		{
			if ($this->logFunc) {
				call_user_func($this->logFunc,'Mail.send failed');
			}
			$this->mail->SmtpClose();
			return false;
		}
		$this->mail->SmtpClose();
		return true;
	}

	/**
	*过滤特殊邮箱设置编码
	*@param str $mail 邮箱
	*@param str $content 邮件内容
	*/
	function filterMail($mail,&$content)
	{
		$star = strpos($mail,'@');
		$end = strpos($mail,'.');
		$tag = substr($mail,$star+1,$end-$star-1);
		if(in_array($tag,array('163')))
		{
			$content = mb_convert_encoding($content,"GB2312","UTF-8");
		}
	}
}
